package com.mgface.eth.api;

import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.admin.methods.response.PersonalUnlockAccount;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
import org.web3j.protocol.http.HttpService;

import java.io.File;
import java.math.BigInteger;

/**
 * @author wanyuxiang
 * @version 1.0
 * @project web3jEthereum
 * @create 2018-06-25 17:55
 **/
public class SendTx {
	static final BigInteger GAS_PRICE = BigInteger.valueOf(22_000_000_000L);
	static final BigInteger GAS_LIMIT = BigInteger.valueOf(4_300_000);
	public static void main(String[] args) throws Exception {
		Admin web3j = Admin.build(new HttpService("http://65.49.135.44:8545"));
		PersonalUnlockAccount personalUnlockAccount = web3j.personalUnlockAccount("0x2ee6e711da829ccfefe11d77c387c4888fc064cb","fc40dc0b755b243216b938956cd3fdd1a1f894a3a72a497a1feab01423ef0feb").send();
		if (personalUnlockAccount.accountUnlocked()){
			System.out.println("账户解锁成功！");
		}
		//获取nonce
		EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
				"0x2ee6e711da829ccfefe11d77c387c4888fc064cb", DefaultBlockParameterName.LATEST).sendAsync().get();

		BigInteger nonce = ethGetTransactionCount.getTransactionCount();
		System.out.println("当前交易的序列号:"+nonce);

		String fileName  = WalletUtils.generateNewWalletFile("Xiang5326865+01",new File("E:\\project"),Boolean.TRUE);
		System.out.println("生成的钱包私钥文件名:"+fileName);

		Credentials credentials = WalletUtils.loadCredentials("Xiang5326865+01", "E:\\project"+fileName);
	}
}
